import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:xin_nuo_pda/bean/lot_entity.dart';
import 'package:xin_nuo_pda/values/colors.dart';
import 'package:xin_nuo_pda/values/constant.dart';
import 'package:xin_nuo_pda/views/SubTitleBar.dart';
import 'package:xin_nuo_pda/views/TextView.dart';
import 'package:xin_nuo_pda/utils/string_extension.dart';
import 'package:xin_nuo_pda/views/edittext/ClearEditText.dart';

import 'AddMaterialInfoController.dart';

class AddMaterialInfoPage extends StatelessWidget {
  final controller = Get.put(AddMaterialInfoController());
  final state = Get.find<AddMaterialInfoController>().state;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text(USER),),
      body: SingleChildScrollView(
        child: Container(
          child: Column(
            children: [
              SubTitleBar("添加物料信息", "提交", (){
                Get.back(result: state.materialList);
              }),
              TextView("批次:", state.lotID),
              SizedBox(height: 1,),
              Container(
                margin: EdgeInsets.all(8),
                alignment: Alignment.centerLeft,
                child: Text(
                  "添加物料信息",
                  style: TextStyle(fontWeight: FontWeight.bold, fontSize: 16),
                ),
              ),
              GetBuilder<AddMaterialInfoController>(
                builder: (controller) => Table(
                  defaultVerticalAlignment: TableCellVerticalAlignment.middle,
                  columnWidths: {
                    //列宽
                    0: FlexColumnWidth(1.0),
                    1: FlexColumnWidth(1.0),
                    2: FlexColumnWidth(1.0),
                    3: FlexColumnWidth(1.0),
                    4: FlexColumnWidth(1.0),
                    5: FlexColumnWidth(1.0)
                  },
                  border: TableBorder.all(
                    color: Colors.grey,
                    width: 1.0,
                    style: BorderStyle.solid,
                  ),
                  children: getMaterialList(context),
                ),

              )
            ],
          ),
        ),
      ),
    );
  }

  getMaterialList(BuildContext context) {
    List<TableRow> tempView = [];
    List<FocusNode> focusNodeList = [];
    List<FocusNode> focusNodeList2 = [];
    tempView.add(TableRow(
        children: [
          //增加行高
          SizedBox(
            height: 40,
            child: Align(
              alignment: Alignment.center,
              child: Text(
                '序号',
                style: TextStyle(
                  color: theme_blue.toColor(),
                ),
              ),
            ),
          ),
          Align(
            alignment: Alignment.center,
            child: Text(
              '物料料号',
              style: TextStyle(
                color: theme_blue.toColor(),
              ),
            ),
          ),
          Align(
            alignment: Alignment.center,
            child: Text(
              '物料类型',
              style: TextStyle(
                color: theme_blue.toColor(),
              ),
            ),
          ),
          Align(
            alignment: Alignment.center,
            child: Text(
              '物料批次',
              style: TextStyle(
                color: theme_blue.toColor(),
              ),
            ),
          ),
          Align(
            alignment: Alignment.center,
            child: Text(
              '加工生产\n批次数量',
              style: TextStyle(
                color: theme_blue.toColor(),
                fontSize: 14,
              ),
            ),
          )
        ],
        //第一行样式 添加背景色
        decoration: BoxDecoration(
          color: table_head.toColor(),
        )));
    for (int i = 0; i < state.materialList.length; i++) {
      LotBOMLINELIST element = state.materialList[i];
      focusNodeList.add(FocusNode());
      focusNodeList2.add(FocusNode());
      tempView.add(TableRow(children: [
        //增加行高
        SizedBox(
          height: 40,
          child: Align(
            alignment: Alignment.center,
            child: Text(
              (i + 1).toString(),
              style: TextStyle(color: text_black.toColor()),
            ),
          ),
        ),
        Align(
          alignment: Alignment.center,
          child: Text(
            element.mATERIALNAME ?? '',
            style: TextStyle(
              color: text_black.toColor(),
            ),
          ),
        ),
        Align(
          alignment: Alignment.center,
          child: Text(
            element.mATERIALTYPE ?? '',
            style: TextStyle(
              color: text_black.toColor(),
            ),
          ),
        ),
        Align(
          alignment: Alignment.topCenter,
          child: Container(
            padding: EdgeInsets.only(bottom: 4),
            child: ClearEditText(
              "请扫描物料批次",
                  (value) {
                    element.MLOTID = value;
              },
              onSubmited: (){
                if(i< state.materialList.length-1){
                  focusNodeList2[i].unfocus();
                  FocusScope.of(Get.context!).requestFocus(focusNodeList2[i+1]);
                }
              },
              isScan: false,
              focusNode: focusNodeList[i],
            isShowIcon: false,),
          ),
        ),
        Align(
          alignment: Alignment.topCenter,
          child: Container(
            padding: EdgeInsets.only(bottom: 4),
            child: ClearEditText(
              "请输入此批物料加工多DIE",
                  (value) {
                    element.PROCESSLOTQTY = value;
              },
              onSubmited: (){
                if(i< state.materialList.length-1){
                  focusNodeList2[i].unfocus();
                  FocusScope.of(Get.context!).requestFocus(focusNodeList2[i+1]);
                }
              },
              isScan: false,
              isShowIcon: false,
              focusNode: focusNodeList2[i],
            ),
          ),
        )
      ]));
    }
    return tempView;
  }
}
